package com.smh.aiagent.tools;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smh.aiagent.mapper.UserInfoMapper;
import com.smh.aiagent.mapper.UserMapper;
import com.smh.aiagent.model.pojo.User;
import com.smh.aiagent.model.pojo.UserInfo;
import lombok.AllArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;

import java.time.format.DateTimeFormatter;
import java.util.HashMap;

/**
 * @author shiminghui
 * @date 2025/6/5 11:47
 * @description: 客户工具
 */
@AllArgsConstructor
public class CustomerTool {

    private final UserMapper userMapper;
    private final UserInfoMapper userInfoMapper;

    @Tool(description = "Get customer information", returnDirect = true) // 返回结果直接作为消息内容
    public String getCustomerInfo(ToolContext toolContext) {
        // 从工具上下文获取参数
        Long userId = (Long) toolContext.getContext().get("userId");
        if (userId == null) {
            throw new RuntimeException("userId is null");
        }

        // 查询用户信息
        User user = userMapper.selectById(userId);
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getUserId, userId));
        // 拼接返回结果
        HashMap<String, Object> userInfoMap = new HashMap<>();
        userInfoMap.put("fullName", userInfo.getFullName());
        userInfoMap.put("phone", user.getPhone());
        userInfoMap.put("email", user.getEmail());
        userInfoMap.put("address", userInfo.getAddress());
        userInfoMap.put("gender", userInfo.getGender());
        userInfoMap.put("birthday", userInfo.getBirthday().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        userInfoMap.put("resume", userInfo.getResume());
        return JSONUtil.toJsonStr(userInfoMap);

    }

}
